﻿using Infrastructure;
using Infrastructure.Cache;
using System;
using System.Collections.Generic;
using System.Text;

namespace FacadeApi.Extension
{
    public static class TokenManager
    {
        static IMemoryCacheService memoryCacheService;
        static TokenManager() {
            memoryCacheService = ServiceProvide.Get<IMemoryCacheService>();
        }
        public static bool ValidToken(int userID, string token)
        {
            var m_token = memoryCacheService.Get<string>($"UserToken:{userID}");

            if (token != m_token) return false;

            return true;
        }

        public static bool ValidDeviceToken(string deviceID, string token)
        {
            var m_token = memoryCacheService.Get<string>($"DeviceToken:{deviceID}");

            if (token != m_token) return false;

            return true;
        }

        public static string GetDeviceToken(string deviceID, TimeSpan t)
        {
            var guid = Guid.NewGuid().ToString("N");

            memoryCacheService.Set<string>($"DeviceToken:{deviceID}", guid, t);

            return guid;
        }
        public static string GetUserToken(int userID, TimeSpan t)
        {
            var guid = Guid.NewGuid().ToString("N");

            memoryCacheService.Set<string>($"UserToken:{userID}", guid, t);

            return guid;
        }
    }
}
